﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PowerCreator.LMS.DL.Project.Entity;
using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Common.Utils;
using PowerCreator.LMS.Web.DL.Project;

public partial class sysadmin_DL_Project_AddAdvancedUser :PowerCreator.LMS.Web.Base.WebBasePage
{
    TOUserGroup objUserGroup = new TOUserGroup();
    TOAdvancedUser obj=new TOAdvancedUser();
    private PowerCreator.LMS.Web.DL.User.LoginEntity login = new PowerCreator.LMS.Web.DL.User.LoginEntity();
    private string url = "../../login.aspx";

    protected void Page_Load(object sender, EventArgs e)
    {

        CheckUser();
        if (!IsPostBack)
        {
            
            InitPage();
            //BinderUserGroup();
            BinderCatalog();
            BinderXueduan();
            BinderRegion();
            int AID = 0; 
            if (Request.QueryString["AID"] != null)
            {
                AID = int.Parse(Request.QueryString["AID"].ToString());
                ViewState["AID"] = AID.ToString();
                BinderForm(AID);
            }
        }
    }

    private void CheckUser()
    {
        if (Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO] == null)
        {
            Script.RedirectNewUrl(url);
        }
        else
        {
            login = (PowerCreator.LMS.Web.DL.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
            if (login == null)
            {
                Script.RedirectNewUrl(url);
            }
            else
            {
                if (login.VOAdvancedUser == null)
                {
                    Script.RedirectNewUrl(url);
                }
                else if (login.VOAdvancedUser.AUserGroup > 2)
                {
                    //Response.Write(login.VOAdvancedUser.AUserGroup.ToString());
                    //Response.End();
                    Script.ShowAndHistory("您无此权限！", -1);
                }
            }
        }

    }

    private void BinderCatalog()
    {
        PowerCreator.LMS.Web.Catalog.Catalog opCatalog = new PowerCreator.LMS.Web.Catalog.Catalog();
        string search = "";
        DataTable dt = opCatalog.QueryCatalog(search);
        ArrayList list = opCatalog.GetCatalogTree(dt);
        ddl_Catalog.DataSource = list;
        ddl_Catalog.DataTextField = "Text";
        ddl_Catalog.DataValueField = "Value";
        ddl_Catalog.DataBind();

        ListItem li = new ListItem();
        li.Text = "不选择";
        li.Value = "0";
        ddl_Catalog.Items.Insert(0, li);

    }
    private void BinderXueduan()
    {
        TOSchoolStage obj = new TOSchoolStage();
        string search = "";
        DataTable dt = obj.Query(search);

        ddl_xueduan.DataSource = dt;
        ddl_xueduan.DataTextField = "SchoolStage";
        ddl_xueduan.DataValueField = "ID";
        ddl_xueduan.DataBind();

        ListItem li = new ListItem();
        li.Text = "不选择";
        li.Value = "0";
        ddl_xueduan.Items.Insert(0, li);
    }
    private void BinderRegion()
    {
        ddl_Region.Items.Clear();
        TORegion objRegion = new TORegion();
        DataTable dt = new DataTable();
        string search = "";

        dt = objRegion.Query(search);
        ddl_Region.DataSource = dt;
        ddl_Region.DataTextField = "Region";
        ddl_Region.DataValueField = "ID";
        ddl_Region.DataBind();

        ListItem li = new ListItem();
        li.Text = "不选择";
        li.Value = "0";
        ddl_Region.Items.Insert(0, li);
    }

    private int CheckPopedom()
    {
        int rt = 0;

        login = (PowerCreator.LMS.Web.DL.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
        if (login != null)
        {
            if (login.VOAdvancedUser == null)
            {
                Script.RedirectNewUrl(url);
            }
            else 
                rt = login.VOAdvancedUser.AUserGroup;
        }
        return rt;


    }
    //绑定用户组
    //private void BinderUserGroup()
    //{
    //    ddl_UserGroup.Items.Clear();
    //    if (CheckPopedom() == 1)
    //    {
    //        ddl_UserGroup.Items.Clear();
    //        ListItem li1 = new ListItem();
    //        li1.Text = "系统管理员";
    //        li1.Value = "1";
    //        ddl_UserGroup.Items.Add(li1);
    //    }
    //    ListItem li2 = new ListItem();
    //    li2.Text = "区域管理员";
    //    li2.Value = "2";
    //    ddl_UserGroup.Items.Add(li2);
    //    ListItem li3 = new ListItem();
    //    li3.Text = "主讲教师";
    //    li3.Value = "3";
    //    ddl_UserGroup.Items.Add(li3);
    //    ListItem li4 = new ListItem();
    //    li4.Text = "班级管理员";
    //    li4.Value = "4";
    //    ddl_UserGroup.Items.Add(li4);
    //    ListItem li5 = new ListItem();
    //    li5.Text = "课程辅导员";
    //    li5.Value = "5";
    //    ddl_UserGroup.Items.Add(li5);

    //    ListItem li6 = new ListItem();
    //    li6.Text = "资源管理员";
    //    li6.Value = "6";
    //    ddl_UserGroup.Items.Add(li6);

    //}

    private void BinderForm(int id)
    {
        VOTOAdvancedUser vo = new VOTOAdvancedUser();
        vo = obj.Load(id);

        txt_UserName.Text = vo.AUserName;
        txt_UserName.Enabled = false;
        txt_UserPwd.Visible = false;
        txt_DBUserPwd.Visible = false;
        tr_dbpwd.Visible = false;
        tr_pwd.Visible = false;
        lbl_AddStudent_msg_username.Visible = false;
        //ddl_UserGroup.SelectedValue = vo.AUserGroup.ToString();
        txt_RealName.Text = vo.Name;
        txt_Email.Text = vo.Email;
        txt_Tel.Text = vo.Mobile;
        txt_Description.Text = vo.Memo;
        ddl_Region.SelectedValue = vo.Region.ToString();
        ddl_xueduan.SelectedValue = vo.XueDuan.ToString();
        ddl_Catalog.SelectedValue = vo.XueKe.ToString();

        if (vo.Sex)
        {
            radbtn_Male.Checked= true;
            radbtn_Female.Checked = false;
        }
        else
        {
            radbtn_Male.Checked = false;
            radbtn_Female.Checked = true;
        }

    }

    protected override void InitPage()
    {
        lbl_InfoTitle_AddTeacher.Text = GetLanageValueByKey("lbl_AddTeacher_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_AddTeacher_Account_loading.Text = GetLanageValueByKey("lbl_AddTeacher_Account_loading", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_AddTeacher_LoadingName.Text = GetLanageValueByKey("lbl_AddTeacher_LoadingName", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_AddTeacher_PassWord.Text = GetLanageValueByKey("lbl_AddTeacher_PassWord", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lblUserLoginIP.Text = GetLanageValueByKey("lbl_AddStudent_IsLoginIP", PowerCreator.LMS.Language.LanagetItemType.Page);

        lbl_AddTeacher_BaseInformaition.Text = GetLanageValueByKey("lbl_AddTeacher_BaseInformaition", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_AddTeacher_Name.Text = GetLanageValueByKey("lbl_AddTeacher_Name", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_AddTeacher_Number.Text = GetLanageValueByKey("lbl_AddTeacher_Number", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_AddTeacher_Sex.Text = GetLanageValueByKey("lbl_AddTeacher_Sex", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_AddTeacher_IdNumber.Text = GetLanageValueByKey("lbl_AddTeacher_IdNumber", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_AddTeacher_Born.Text = GetLanageValueByKey("lbl_AddTeacher_Born", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_AddTeacher_Mail.Text = GetLanageValueByKey("lbl_AddTeacher_Mail", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_AddTeacher_Class.Text = GetLanageValueByKey("lbl_AddTeacher_Class", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_AddTeacher_Time.Text = GetLanageValueByKey("lbl_AddTeacher_Time", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_AddTeacher_Job.Text = GetLanageValueByKey("lbl_AddTeacher_Job", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_AddTeacher_Duty.Text = GetLanageValueByKey("lbl_AddTeacher_Duty", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_AddTeacher_Phone.Text = GetLanageValueByKey("lbl_AddTeacher_Phone", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_AddTeacher_Native.Text = GetLanageValueByKey("lbl_AddTeacher_Native", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_AddTeacher_Post.Text = GetLanageValueByKey("lbl_AddTeacher_Post", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_AddTeacher_MobileTelephone.Text = GetLanageValueByKey("lbl_AddTeacher_MobileTelephone", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_AddTeacher_Address.Text = GetLanageValueByKey("lbl_AddTeacher_Address", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_AddTeacher_Remark.Text = GetLanageValueByKey("lbl_AddTeacher_Remark", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_AddTeacher_School.Text = GetLanageValueByKey("lbl_AddTeacher_School", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lbl_AddTeacher_XueLi.Text = GetLanageValueByKey("lbl_AddTeacher_XueLi", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lab_Birth_css.Text = "(格式：1980-01-01)";
        //lab_JoinTime.Text = "(格式：1980-01-01)";
        radbtn_Male.Text = GetLanageValueByKey("EditUser_desc27", PowerCreator.LMS.Language.LanagetItemType.Page);
        radbtn_Female.Text = GetLanageValueByKey("EditUser_desc28", PowerCreator.LMS.Language.LanagetItemType.Page);
        rfv_txtUserName.ErrorMessage = GetLanageValueByKey("AddTeacher_msg_g1", PowerCreator.LMS.Language.LanagetItemType.Page);
        rfv_txtRealName.ErrorMessage = GetLanageValueByKey("AddTeacher_msg_g2", PowerCreator.LMS.Language.LanagetItemType.Page);
        ftv_txtUserPwd.ErrorMessage = GetLanageValueByKey("AddTeacher_msg_g4", PowerCreator.LMS.Language.LanagetItemType.Page);

        //btn_AddNew.Text = GetLanageValueByKey("btn_AddNew", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_Reset.Text = GetLanageValueByKey("btn_Reset", PowerCreator.LMS.Language.LanagetItemType.Page);
        base.InitPage();
    }
    private bool CheckPageData()
    {
        string search = string.Format("AUserName = '{0}'", txt_UserName.Text);
        if (ViewState["AID"] != null)
        {
            search += "and AUser_Id<>" + ViewState["AID"].ToString();
        }
        if (obj.Count(search) != 0)
        {
            lbl_msg_Description.Text = GetLanageValueByKey("AddTeacher_msg_g3", PowerCreator.LMS.Language.LanagetItemType.Page);
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "showmy1", "<script>AutoColseAler()</script>");
            return false;
        }
       
        if (!string.IsNullOrEmpty(txt_Email.Text))
        {
            if (!PageValidate.IsEmail(txt_Email.Text.Trim()))
            {
                lbl_msg_Description.Text = GetLanageValueByKey("EditorTeacher_msg_g3", PowerCreator.LMS.Language.LanagetItemType.Page);
                txt_Email.BackColor = System.Drawing.Color.BlanchedAlmond;
                txt_Email.Focus();
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "showmy1", "<script>AutoColseAler()</script>");
                return false;
            }
        }
      
        if (!string.IsNullOrEmpty(txt_Tel.Text.Trim()))
        {
            if (!PageValidate.IsNumber(txt_Tel.Text))
            {
                lbl_msg_Description.Text = GetLanageValueByKey("EditorTeacher_msg_g5", PowerCreator.LMS.Language.LanagetItemType.Page);
                txt_Tel.BackColor = System.Drawing.Color.BlanchedAlmond;
                txt_Tel.Focus();
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "showmy1", "<script>AutoColseAler()</script>");
                return false;
            }
        }
        return true;
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (!CheckPageData()) return;
        VOTOAdvancedUser vo = new VOTOAdvancedUser();

        if (ViewState["AID"] == null)
        {
            vo.AUserName = txt_UserName.Text.Trim();
            vo.AUserPwd = PowerCreator.LMS.Common.Utils.MD5Helper.GetMD5(txt_UserPwd.Text.Trim());
            //vo.AUserGroup = int.Parse(ddl_UserGroup.SelectedValue);
            vo.Email = txt_Email.Text.Trim();
            vo.Mobile = txt_Tel.Text.Trim();
            vo.Name = txt_RealName.Text.Trim();
            if (radbtn_Female.Checked == true)
                vo.Sex = false;
            if (radbtn_Male.Checked == true)
                vo.Sex = true;

            vo.AddDate = DateTime.Now;
            vo.LoginNum = 0;
            vo.Memo = txt_Description.Text.Trim();
            vo.Region = int.Parse(ddl_Region.SelectedValue);
            vo.XueDuan = int.Parse(ddl_xueduan.SelectedValue);
            vo.XueKe = int.Parse(ddl_Catalog.SelectedValue);
            //vo.AUserGroup = int.Parse(ddl_UserGroup.SelectedValue);
            if (obj.Add(vo))
            {
                string url = "ManageAdvancedUser.aspx";
                Script.RedirectURL(url);
            }
        }
        else
        {
            vo = obj.Load(int.Parse(ViewState["AID"].ToString()));
            //vo.AUserName = txt_UserName.Text.Trim();
            //vo.AUserPwd = txt_UserPwd.Text.Trim();
            //vo.AUserGroup = int.Parse(ddl_UserGroup.SelectedValue);
            vo.Email = txt_Email.Text.Trim();
            vo.Mobile = txt_Tel.Text.Trim();
            vo.Name = txt_RealName.Text.Trim();
            if (radbtn_Female.Checked == true)
                vo.Sex = false;
            if (radbtn_Male.Checked == true)
                vo.Sex = true;

            vo.AddDate = DateTime.Now;
            vo.LoginNum = 0;
            vo.Memo = txt_Description.Text.Trim();
            vo.Region = int.Parse(ddl_Region.SelectedValue);
            vo.XueDuan = int.Parse(ddl_xueduan.SelectedValue);
            vo.XueKe = int.Parse(ddl_Catalog.SelectedValue);

            if (obj.Update(vo))
            {
                string url = "ManageAdvancedUser.aspx";
                Script.RedirectURL(url);
            }
        }
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        string url = "ManageAdvancedUser.aspx";
        Script.RedirectURL(url);
    }
}
